package com.navi.mould1.service.impl;

import com.navi.bean.in.ConditionBean;
import com.navi.bean.out.powerTrend_chart_report.PowerTrendChartReportGridOut;
import com.navi.bean.sql.SqlVo;
import com.navi.mould1.service.IPowerTrendChartReportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import static com.navi.bean.sql.SqlVo.jointInSql;
import static com.navi.core.util.JdbcUtils.sqlServer_QueryRunner_QueryListBySql;


@Slf4j
@Service
public class PowerTrendChartReportServiceImpl implements IPowerTrendChartReportService {


    public List<PowerTrendChartReportGridOut> getGridData(ConditionBean conditions) {
        final SqlVo sqlVo = new SqlVo();
        sqlVo.appendSql("SELECT");
        sqlVo.appendSql("test_time, pm");
        sqlVo.appendSql("FROM (");
        sqlVo.appendSql("SELECT");
        sqlVo.appendSql("ROW_NUMBER() OVER(PARTITION BY lot_number ORDER BY bom_material_code) as row_number,");
        sqlVo.appendSql("*");
        sqlVo.appendSql("FROM dbo.iv_distribution");
        sqlVo.appendSql("WHERE 1=1");
        if (StringUtils.isNotEmpty(conditions.getBay_id())) sqlVo.appendSql("AND equipment_code in" + jointInSql(conditions.getBay_id()));
        //if (StringUtils.isNotEmpty(conditions.getComponent_cell_efficiency())) sqlVo.appendSql("AND attr_1 in" + jointInSql(conditions.getComponent_cell_efficiency()));
        if (StringUtils.isNotEmpty(conditions.getComponent_cell_efficiency())) sqlVo.appendSql(Arrays.asList(conditions.getComponent_cell_efficiency().split(",")).stream().map(data -> "attr_1 like '%" + data + "%'").collect(Collectors.joining(" OR ", " AND (", ")")));
        if (StringUtils.isNotEmpty(conditions.getStart_time())) sqlVo.appendSqlAndAddParam("AND test_time >= ?", conditions.getStart_time());
        if (StringUtils.isNotEmpty(conditions.getEnd_time())) sqlVo.appendSqlAndAddParam("AND test_time < ?", conditions.getEnd_time());
        sqlVo.appendSql(") AS tbl");
        sqlVo.appendSql("WHERE tbl.row_number = 1");
        sqlVo.appendSql("ORDER BY tbl.test_time");

        return sqlServer_QueryRunner_QueryListBySql(sqlVo, PowerTrendChartReportGridOut.class);
    }

}
